package com.xinke.edu.mypetadoption;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xinke.edu.mypetadoption.databinding.ActivityMain2Binding;
import com.xinke.edu.mypetadoption.fragment2.DdFragment;
import com.xinke.edu.mypetadoption.fragment2.SpFragment;
import com.xinke.edu.mypetadoption.fragment2.SzFragment;
import com.xinke.edu.mypetadoption.fragment2.TjFragment;

public class Main2Activity extends AppCompatActivity {

    private ActivityMain2Binding binding;

    // Fragment
    private SpFragment spFragment;
    private DdFragment ddFragment;
    private TjFragment tjFragment;
    private SzFragment szFragment;
    private Fragment activeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // DataBinding
        binding = ActivityMain2Binding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());

        // 初始化 Fragment
        initFragments();

        // 设置底部导航点击事件
        binding.navgationview.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            Log.d("Main2Activity", "点击菜单ID: " + itemId);

            if (itemId == R.id.sp) {
                switchFragment(spFragment);
                return true;
            } else if (itemId == R.id.dd) {
                switchFragment(ddFragment);
                return true;
            } else if (itemId == R.id.tj) {
                switchFragment(tjFragment);
                return true;
            } else if (itemId == R.id.sz) {
                switchFragment(szFragment);
                return true;
            }
            return false;
        });
    }

    private void initFragments() {
        spFragment = new SpFragment();
        ddFragment = new DdFragment();
        tjFragment = new TjFragment();
        szFragment = new SzFragment();

        // 默认显示 spFragment
        activeFragment = spFragment;

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(binding.fragment.getId(), spFragment, "SP");
        transaction.add(binding.fragment.getId(), ddFragment, "DD").hide(ddFragment);
        transaction.add(binding.fragment.getId(), tjFragment, "TJ").hide(tjFragment);
        transaction.add(binding.fragment.getId(), szFragment, "SZ").hide(szFragment);
        transaction.commit();
    }

    private void switchFragment(Fragment fragment) {
        if (fragment == activeFragment) return;

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(activeFragment).show(fragment).commitAllowingStateLoss();
        activeFragment = fragment;

        Log.d("Main2Activity", "当前显示 Fragment: " + fragment.getClass().getSimpleName());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
